# -*- coding: utf-8 -*-
from db.mysql import MysqlClient
from common.helper import Helper
from itertools import product
from urllib.parse import urlencode
import requests
from common.config import *

class Arrangement(object):
    def __init__(self):
        self.mysql = MysqlClient()
        self.name = "个人重大疾病保险"

    # 生产产品数据
    def genProductInfo(self):
        plan = {
            "planA": {
                "year": ["18-29岁", "30-39岁", "40-49岁"],
                "gender": ["M", "F"],
                "expire": ["1年"],
                "payType": ["年交"],
                "plan": ["经典款"],
                "majorDisease": ["10w"],
                "generalHospital": ["80/天"],
                "seriousHospital": ["120/天"]
            },
            "planB": {
                "year": ["18-29岁", "30-39岁", "40-49岁"],
                "gender": ["M", "F"],
                "expire": ["1年"],
                "payType": ["年交"],
                "plan": ["豪华款"],
                "majorDisease": ["20w"],
                "generalHospital": ["100/天"],
                "seriousHospital": ["150/天"]
            },
            "planC": {
                "year": ["18-29岁", "30-39岁", "40-49岁"],
                "gender": ["M", "F"],
                "expire": ["1年"],
                "payType": ["年交"],
                "plan": ["豪华款"],
                "majorDisease": ["30w"],
                "generalHospital": ["150/天"],
                "seriousHospital": ["225/天"]
            }

        }
        for k in plan:
            for v in product(plan[k]["year"], plan[k]["gender"], plan[k]["expire"], plan[k]["payType"], plan[k]["plan"],
                             plan[k]["majorDisease"], plan[k]["generalHospital"], plan[k]["seriousHospital"]):
                data = {
                    "year":v[0],
                    "gender":v[1],
                    "expire":v[2],
                    "payType":v[3],
                    "plan":v[4],
                    "majorDisease":v[5],
                    "generalHospital":v[6],
                    "seriousHospital":v[7]
                }
                yield  data

    # 执行爬虫
    def requestJson(self):

        for v in self.genProductInfo():

            if v["plan"] == "经典款":
                data = {
                    "requestData":{"productId": "1226_zdjb", "isMulti": "1", "issueChannel": "1",
                                  "list": [{"cInsrncCde": "120010", "nSelectFlag": "1", "nPrm": "90", "nAmt": "10万"},
                                           {"cInsrncCde": "120004", "nSelectFlag": "1", "nPrm": "12", "nAmt": "80元/天"},
                                           {"cInsrncCde": "120005", "nSelectFlag": "1", "nPrm": "0", "nAmt": "120元/天"}],
                                  "productPlanId": "0fa5ca9f-eb21-4531-8234-2b47a6461607", "personSex": "106001" if v["gender"] == "M" else "106002" ,
                                  "personAge": v["year"].replace("岁","")}
                }

            elif v["plan"] == "豪华版":

                data = {

                    "requestData": {"productId": "1226_zdjb", "isMulti": "1", "issueChannel": "1",
                                    "list": [{"cInsrncCde": "120010", "nSelectFlag": "1", "nPrm": "150", "nAmt": "20万"},
                                             {"cInsrncCde": "120004", "nSelectFlag": "1", "nPrm": "15", "nAmt": "100元/天"},
                                             {"cInsrncCde": "120005", "nSelectFlag": "1", "nPrm": "0", "nAmt": "150元/天"}],
                                    "productPlanId": "f9ce684c-82e3-4e11-9406-d78d3f4b902d", "personSex": "106001" if v["gender"] == "M" else "106002" ,
                                    "personAge": v["year"].replace("岁","")}
                }
            else:
                data = {
                    "requestData":{"productId": "1226_zdjb", "isMulti": "1", "issueChannel": "1",
                                  "list": [{"cInsrncCde": "120010", "nSelectFlag": "1", "nPrm": "210", "nAmt": "30万"},
                                           {"cInsrncCde": "120004", "nSelectFlag": "1", "nPrm": "22.5",
                                            "nAmt": "150元/天"},
                                           {"cInsrncCde": "120005", "nSelectFlag": "1", "nPrm": "0", "nAmt": "225元/天"}],
                                  "productPlanId": "28ba39a0-1578-4f85-9de6-2b517d9c31ed", "personSex": "106001" if v["gender"] == "M" else "106002" ,
                                  "personAge": v["year"].replace("岁","")}
                }
            payload = urlencode(data).replace("+","")
            r = requests.post( url = domain[self.name]["url"],headers=domain[self.name]["header"],data=payload )
            if r.status_code == 200 and "交易成功" in r.text:

                price = r.json()["premium"]
                data = {
                    "name":self.name,
                    "company":"华安保险",
                    "year":v["year"],
                    "gender":"0" if v["gender"] == "F" else "1",
                    "expire":v["expire"],
                    "payType":v["payType"],
                    "majorDisease":v["majorDisease"],
                    "generalHospital":v["generalHospital"],
                    "seriousHospital":v["seriousHospital"],
                    "price":price
                }
                Helper.mergeTable( self.name,self.mysql,data )

